package diona.emotions

import com.aliasi.classify._
import java.lang.RuntimeException
import kinopoisk.KinopoiskTrain

/**
 * User: anton
 * Date: 20.06.11 23:51
 */
object PolarityClassifier
{
  private val classifier = DynamicLMClassifier.createNGramProcess(Array("pos", "neg"), 8)

  private lazy val trained = train(KinopoiskTrain)

  private def train(trainsSource: TrainSource) : Boolean = {
    train(new Classification("pos"), trainsSource.getPositiveExamples)
    train(new Classification("neg"), trainsSource.getNegativeExamples)
    true
  }

  private def train(classification: Classification, trainData: Iterator[String])
  {
    for {
      text <- trainData
    } {
      classifier.handle(new Classified[CharSequence](text, classification))
    }
  }

  def classify(text: String) : String = {
    if (!trained) {
      throw new RuntimeException("Not trained")
    }
    val classification: JointClassification = classifier.classify(TextUtil.clearText(text))
    classification.bestCategory()
  }
}